package com.game.server.message.proto.execute;

import com.comm.ISession;
import com.game.majiang.config.DataManager;
import com.game.majiang.config.LogManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.majiang.vo.UserVo;
import com.game.server.db.entity.UserInfoData;
import com.game.server.message.proto.in.AbstractUpdateuserReq;
import com.game.server.message.proto.out.UpdateuserRsp;

public class UpdateuserReq extends AbstractUpdateuserReq {

    @Override
    public void Execute(ISession session) {
		LogManager.getInstance().Log("更新用户");
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
        	UpdateuserRsp rsp=new UpdateuserRsp();
        	rsp.setAutograph(this.autograph);
        	rsp.setHead(this.head);
        	
        	rsp.setNickname(this.nickname);
        	rsp.setSex(this.sex);
//        	if(!Util.judgeChineseStr(autograph))
//        	{
        		boolean updateNameBool = true;
        		if (client.getVo().getNickName().equals(this.nickname)) {
        			updateNameBool = false;
				}
        		if (updateNameBool) {
					rsp.setIsok(3);
        			boolean checkNickname = UserVo.checkNickname(this.nickname,client.getUserId());
        			if(!checkNickname) {
						rsp.setIsok(1);
						int checkNameupdate = UserVo.checkNameupdate(client.getUserId());
						if (checkNameupdate>0){
							UserVo.updateNameupdateMinus(client.getUserId());
							client.getVo().setNameupdate(client.getVo().getNameupdate()-1);
							updateNameBool = false;
						}else{
							String informationupdateconsumption = DataManager.getInstance().getSys_parameter().get("informationupdateconsumption");
							int consumptionIntegral = Integer.parseInt(informationupdateconsumption);
							if (client.getVo().getIntegral() >= consumptionIntegral) {
								client.getVo().AddIntegral(-consumptionIntegral);
								updateNameBool = false;
							}
						}
        			}
				}
				if (!updateNameBool){
					rsp.setIsok(2);
					boolean bool = UserVo.updateUser(client.getUserId(),this.sex,this.autograph, this.nickname, this.head);
					if(bool){
						client.getVo().setSex(this.sex);
						client.getVo().setAutograph(this.autograph);
						client.getVo().setHead(this.head);
						client.getVo().setNickName(this.nickname);
						rsp.setIsok(0);
						client.getVo().getuserinfo(client);

						UserInfoData infoData = DataManager.getInstance().getMapUserData().get(client.getUserId());
						infoData.setName(this.nickname);
						infoData.setHead(this.head);
						infoData.setSex(this.sex);
						infoData.setAutograph(this.autograph);
					}
				}

//        	}
        	client.Send(rsp);
        }
    }

}
